package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.DefineIDZone;
import com.syxp.pda.entity.base.DeviceBaseInfo;
import com.syxp.pda.service.base.DefineIDZoneService;
import com.syxp.pda.service.base.DeviceBaseInfoService;

public class ZoneDeviceAction extends BaseAction {
	@Resource
	private DefineIDZoneService defineIDZoneService;
	@Resource
	private DeviceBaseInfoService deviceBaseInfoService;
	private DeviceBaseInfo deviceBaseInfo;
	private String ids;
	private int start ;
	
	private int limit ;
	
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public DeviceBaseInfoService getDeviceBaseInfoService() {
		return deviceBaseInfoService;
	}

	public void setDeviceBaseInfoService(DeviceBaseInfoService deviceBaseInfoService) {
		this.deviceBaseInfoService = deviceBaseInfoService;
	}

	public DeviceBaseInfo getDeviceBaseInfo() {
		return deviceBaseInfo;
	}

	public void setDeviceBaseInfo(DeviceBaseInfo deviceBaseInfo) {
		this.deviceBaseInfo = deviceBaseInfo;
	}

	public DefineIDZoneService getDefineIDZoneService() {
		return defineIDZoneService;
	}

	public void setDefineIDZoneService(DefineIDZoneService defineIDZoneService) {
		this.defineIDZoneService = defineIDZoneService;
	}

	private int zoneID;//传入的参数
	
	public int getZoneID() {
		return zoneID;
	}

	public void setZoneID(int zoneID) {
		this.zoneID = zoneID;
	}

	public DefineIDZone getZone() {
		return zone;
	}

	public void setZone(DefineIDZone zone) {
		this.zone = zone;
	}

	private DefineIDZone zone; //注入Station实体类
	public String save(){ //保存
		try{
			System.out.println(zone) ;
			DefineIDZone s= defineIDZoneService.addService(zone) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("zone.zoneID", String.valueOf(zone.getZoneID())) ;
			jobj.addProperty("zone.idCardInfo", zone.getIdCardInfo()) ;
			jobj.addProperty("zone.zoneName", zone.getZoneName()) ;
			jobj.addProperty("zone.zonePlace", zone.getZonePlace()) ;
			jobj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
	public String loadZoneTree(){
		List<DefineIDZone> all=defineIDZoneService.getDao().getAll();
		JsonArray jarray = new JsonArray() ;
		JsonObject result = new JsonObject() ;
		for(DefineIDZone zone : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("id", zone.getZoneID()) ;
			//jobj.addProperty("idCardInfo", zone.getIdCardInfo()) ;
			jobj.addProperty("text", zone.getZoneName()) ;
		//	jobj.addProperty("zonePlace", String.valueOf(zone.getZonePlace())) ;
			jobj.addProperty("leaf", true);
			jarray.add(jobj) ;
		}
		result.add("result", jarray) ;
		this.jsonString = gson.toJson(jarray) ;
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	public String load() {  //通过穿参数去加载
		try{
		List<DeviceBaseInfo> all=null;
		System.out.println("sdfsdfdfdf-----"+zoneID);
		QueryFilter filter = new QueryFilter() ;
		filter.addFilter(new FieldBean("zoneID" ,zoneID ,Operation.EQUAL));
		filter.setPagingBean(new PagingBean(start,limit)) ;
		all = deviceBaseInfoService.getDao().getAll(filter);
		JsonArray jarray = new JsonArray() ;
		JsonObject result = new JsonObject() ;
		for(DeviceBaseInfo deviceBaseInfo : all){
			JsonObject jobj=new JsonObject();
			jobj.addProperty("deviceInfoID", String.valueOf(deviceBaseInfo.getDeviceInfoID()));
			jobj.addProperty("deviceName", deviceBaseInfo.getDeviceName());
			jobj.addProperty("place", deviceBaseInfo.getDevBuildPlace());
			jobj.addProperty("department", deviceBaseInfo.getDevBuildCompany());
			jobj.addProperty("useDepartment", deviceBaseInfo.getDevUseDepart());
			jarray.add(jobj);
		}
		result.addProperty("totalProperty",deviceBaseInfoService.getDao().getTotalProperty(filter)) ;
		result.add("result", jarray) ;
		result.addProperty("start", start);
		result.addProperty("limit", limit) ;
		this.jsonString="{success:true}" ;
		this.jsonString = gson.toJson(result) ;
		System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
	public String loadAll(){  //加载所有的信息
		try{
			List<DeviceBaseInfo> all=null;
			QueryFilter filter = new QueryFilter() ;
			if(zoneID==0){
				filter.setPagingBean(new PagingBean(start,limit)) ;
				 all=deviceBaseInfoService.getDao().getAll(filter);
			}else{
				filter.setPagingBean(new PagingBean(start,limit)) ;
				filter.addFilter(new FieldBean("zoneID" ,zoneID ,Operation.EQUAL));
				all = deviceBaseInfoService.getDao().getAll(filter);
			}
			JsonArray jarray = new JsonArray() ;
			JsonObject result = new JsonObject() ;
			for(DeviceBaseInfo deviceBaseInfo : all){
				JsonObject jobj=new JsonObject();
				jobj.addProperty("deviceInfoID", String.valueOf(deviceBaseInfo.getDeviceInfoID()));
				jobj.addProperty("deviceName", deviceBaseInfo.getDeviceName());
				jobj.addProperty("place", deviceBaseInfo.getDevBuildPlace());
				jobj.addProperty("department", deviceBaseInfo.getDevBuildCompany());
				jobj.addProperty("useDepartment", deviceBaseInfo.getDevUseDepart());
				jarray.add(jobj);
			}
			result.addProperty("totalProperty",deviceBaseInfoService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString = gson.toJson(result) ;
			System.out.println(jsonString) ;
			return SUCCESS ;
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String delete() {  //删除
		System.out.println(zoneID);
		try{
			defineIDZoneService.removeService(zoneID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	public String get()	{  //查看
		try{
			DefineIDZone z = defineIDZoneService.loadService(zoneID) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("zoneID", z.getZoneID()) ;
			jobj.addProperty("idCardInfo", z.getIdCardInfo()) ;
			jobj.addProperty("zoneName", z.getZoneName()) ;
			jobj.addProperty("zonePlace", z.getZonePlace()) ;
			jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jobj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			System.out.println(ids);
			GsonBuilder builder = new GsonBuilder();
			  Gson gson = builder.create();
			  List<DeviceBaseInfo> 
			  all = gson.fromJson(ids, new TypeToken<List<DeviceBaseInfo>>() {}.getType());
			  for(DeviceBaseInfo deviceBaseInfo : all){
				  System.out.println("zongID:   " + deviceBaseInfo.getZoneID()) ;
				  deviceBaseInfoService.updateDeviceZone(deviceBaseInfo) ;		
			  }
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
}
